//
//  WSAnswerViewController.m
//  WisdomStar
//
//  Created by 谭飞 on 2018/10/31.
//  Copyright © 2018年 zhangliwen. All rights reserved.
//

#import "WSAnswerViewController.h"
#import "WSQuestionListViewController.h"
#import "SHPickerView.h"

#import "CourseService.h"

@interface WSAnswerViewController ()

@property (nonatomic, strong) UIButton *rightNavButton;
@property (weak, nonatomic) IBOutlet UIView *typeView;
@property (weak, nonatomic) IBOutlet UITextView *textView;
@property (weak, nonatomic) IBOutlet UIStackView *stackView;
@property (weak, nonatomic) IBOutlet UIView *contentView;
@property (weak, nonatomic) IBOutlet UILabel *typeLab;
@property (weak, nonatomic) IBOutlet UIButton *radioBtn;

@property (nonatomic, strong) UIButton *selectBtn;

@property (nonatomic, strong) NSArray *cates;
@property (nonatomic, assign) BOOL reward; // 是否悬赏
@property (nonatomic, copy) NSString *cate_id; // 类型id

@end

@implementation WSAnswerViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setup];
    [self setupNavButton];
    [self loadData];
}

- (void)setup {
    self.tableView.contentInset = UIEdgeInsetsMake(TOPBARHEIGHT, 0, 0, 0);
    self.tableView.tableFooterView = [UIView new];
    
    [self.typeView setRoundBorder:kSeparatorColor radius:3.f];
    self.textView.placeHolderText = @"请输入详细内容...";
    self.textView.placeHolderColor = kFontColorGray;
    
    self.contentView.layer.borderColor = kSeparatorColor.CGColor;
    self.contentView.layer.borderWidth = 1.f;
    self.selectBtn = self.radioBtn;
    
    WEAKSELF
    [self.typeView addGestureWithGestureType:WJ_TapGesture completeHandler:^{
        NSMutableArray *items = [NSMutableArray arrayWithCapacity:weakSelf.cates.count];
        [weakSelf.cates enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [items addObject:obj[@"name"]];
        }];
        SHPickerView *pickerView = [[SHPickerView alloc] initWithTitle:@"问题类型" items:items block:^(SHPickerView *pickerView, NSInteger selectRow) {
            weakSelf.typeLab.text = items[selectRow];
            weakSelf.cate_id = weakSelf.cates[selectRow][@"id"];
        }];
        [pickerView show];
    }];
}

- (void)setupNavButton {
    UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.rightNavButton];
    self.navigationItem.rightBarButtonItem = rightButtonItem;
}

- (void)loadData {
    [CourseService fetchCourseCateWithResult:^(id  _Nonnull responseObj) {
        ResponseModel *response = [ResponseModel yy_modelWithJSON:responseObj];
        if (!response.data) {
            return;
        }
        NSMutableArray *cateArr = [NSMutableArray array];
        [response.data enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [cateArr addObject:obj];
        }];
        self.cates = [cateArr copy];
    }];
}

#pragma mark - Actions

- (IBAction)radioBtnClick:(UIButton *)sender {
    if (self.selectBtn == sender) return;
    sender.selected = YES;
    self.selectBtn.selected = NO;
    self.selectBtn = sender;
    self.reward = sender.isSelected;
}


- (void)sendAction {
    [self performSegueWithIdentifier:@"QuestionList" sender:nil];
}

#pragma mark - Getter

- (UIButton *)rightNavButton {
    if (_rightNavButton == nil) {
        _rightNavButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [_rightNavButton setTitle:@"发布" forState:UIControlStateNormal];
        [_rightNavButton setTitleColor:COLOR_HEX(0xE5FFFB) forState:UIControlStateNormal];
        [_rightNavButton.titleLabel setFont:WSFontMake(15)];
        _rightNavButton.xmg_height = kNavBarHeight;
        [_rightNavButton addTarget:self action:@selector(sendAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _rightNavButton;
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
